<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
header('Content-type:text/html;charset=utf-8');

class BettingController extends AdminbaseController{
	public function index(){
		$caizhong = I("caizhong");
		$kaishi = I("kaishi",date("Y-m-d 00:00"));
		$jieshu = I("jieshu",date("Y-m-d 23:59"));
		$leixing = I("leixing");
		$accounts = trim(I("accounts"));

		$this->assign("shijian",$shijian);
		$this->assign("leixing",$leixing);
		$p = I("p");
		$where = array();

		if(!empty($accounts)){
			$dat = M("Accountsinfo")->where("accounts like '%{$accounts}%'")->field("userid")->select();
		}
		 foreach($dat as $v){
			 $uids[] = $v['userid'];
		 }

		if(!empty($caizhong)){
			$where['lotteryclass'] = $caizhong;
		}
		if(!empty($uids)){
			$where['uid'] = array("in",$uids);
		}
		//时间判断
		if($shijian == ''){
			$shijian = '1';
		}
		if(empty($kaishi)||empty($kaishi)){
			$kaishi = date('Y-m-d')." 00:00:00";  //今天开始时间
			$jieshu = date('Y-m-d')." 23:59:59";//今天结束时间
		}
		$stimes = strtotime($kaishi);
		$etimes = strtotime($jieshu);
	   	$where['ctime'] = array(array('egt',$stimes),array('elt',$etimes));
	   	$this->assign("kaishi",$kaishi);
	   	$this->assign("jieshu",$jieshu);

		//类型判断
		if($leixing == ''){
			$leixing = '0';
		}
		if($leixing == '0'){
			$this->assign("leixing",$leixing);
		}elseif($leixing == '1'){
			$where['zong_money'] = array('gt',0);
			$this->assign("leixing",$leixing);
		}elseif($leixing == '2'){
			$where['opencode'] = array('neq',"");
			$where['zong_money'] = array('eq',0);
			$this->assign("leixing",$leixing);
		}elseif($leixing == '3'){
			$where['opencode'] = array('eq',"");
			$this->assign("leixing",$leixing);
		}
		//搜索彩票名称
		$classs = M('lottery_class')->select();
		//列表
		$data1 = M('lottery_add')->where($where) ->select();
		$data2 = M('lottery_11x5')->where($where)->select();
	    $data3 = M('lottery_fc3d')->where($where)->select();
	    $data4 = M('lottery_kl8')->where($where) ->select();
	    $data5 = M('lottery_pk10')->where($where)->select();
		$data6 = M('lottery_ssc')->where($where)->select();
		   foreach($data1 as $k=>$v){
			   $data1[$k]['table'] = 1;
		   }
		   foreach($data2 as $k=>$v){
			   $data2[$k]['table'] = 2;
		   }
		   foreach($data3 as $k=>$v){
			   $data3[$k]['table'] = 3;
		   }
		   foreach($data4 as $k=>$v){
			   $data4[$k]['table'] = 4;
		   }
		   foreach($data5 as $k=>$v){
			   $data5[$k]['table'] = 5;
		   }
		   foreach($data6 as $k=>$v){
			   $data6[$k]['table'] = 6;
		   }
	    $data = array_merge($data1,$data2,$data3,$data4,$data5,$data6);

	    foreach ($data as $user) {
			$ages[] = $user['ctime'];
		}
		array_multisort($ages, SORT_DESC, $data);
		//分页 begion
	   	$count = count($data);
	   	if($count==0){
		   $p = 0;
	   	}else{
		   if(empty($p)){
			   $p = 1;
		   }
	   	}
		$data = array_slice($data,($p-1)*10,10);
	    $totalpage = ceil($count/10);
	    $page = $this->page($count,10);

		$touzhu = 0;   //投注钱数
		$zhongjiang = 0; //中奖钱数
		$yingli = 0;//盈利钱数
		foreach($data as $v){
			$touzhu += $v["money"];
			$zhongjiang += $v["zong_money"];
			$yingli = $zhongjiang-$touzhu;
		}

		foreach($data as $v){
			$lotteryclass[] = $v["lotteryclass"]; //彩种
			$ids[] = $v["id"];  //内容
			$uid[] = $v['uid'];
		}
         $uids = implode(",",$uid);

		if($uids){
			$yonghu = M('Accountsinfo')->where(" userid in ($uids) ")->field("userid,accounts")->select();
		}

       foreach($data as $k=>$v){
		   foreach($yonghu as $kk=>$vv){
			   if($v['uid'] == $vv['userid']){
				   $data[$k]['mingzi'] = $vv['accounts'];
			   }
		   }
	   }


		//彩种
		$lotteryclasss = implode(",",$lotteryclass);
		if($lotteryclasss){
			$class = M('lottery_class')->where("id in($lotteryclasss)")->select();
		}
		//内容
		$idss = implode(",",$ids);
		if($idss){
			$lottery_add_detail  = M('lottery_add_detail')->where("lottery_add_id in($idss)")->field("lottery_add_id,cal,is_zhong")->select();
			$lottery_11x5_detail = M('lottery_11x5_detail')->where("lottery_11x5_id in($idss)")->field("lottery_11x5_id,cal,is_zhong")->select();
			$lottery_fc3d_detail = M('lottery_fc3d_detail')->where("lottery_fc3d_id in($idss)")->field("lottery_fc3d_id,cal,is_zhong")->select();
			$lottery_kl8_detail = M('lottery_kl8_detail')->where("lottery_kl8_id in($idss)")->field("lottery_kl8_id,cal,is_zhong")->select();
			$lottery_pk10_detail = M('lottery_pk10_detail')->where("lottery_pk10_id in($idss)")->field("lottery_pk10_id,cal,is_zhong")->select();
			$lottery_ssc_detail = M('lottery_ssc_detail')->where("lottery_ssc_id in($idss)")->field("lottery_ssc_id,cal,is_zhong")->select();
		}
		$a1 = array();
		foreach($lottery_add_detail as $v){
			//内容
			if(isset($a1[$v["lottery_add_id"]])){
				$a1[$v["lottery_add_id"]] = $a1[$v["lottery_add_id"]].'#'.$v["cal"];
			}else{
				$a1[$v["lottery_add_id"]] = $v["cal"];
			}
		}
		$a2 = array();
	    foreach($lottery_11x5_detail as $v){
		   //内容
		   if(isset($a2[$v["lottery_11x5_id"]])){
			   $a2[$v["lottery_11x5_id"]] = $a2[$v["lottery_11x5_id"]].'#'.$v["cal"];
		   }else{
			   $a2[$v["lottery_11x5_id"]] = $v["cal"];
		   }
	   	}
	   	$a3 = array();
	   	foreach($lottery_fc3d_detail as $v){
		   //内容
		   if(isset($a3[$v["lottery_fc3d_id"]])){
			   $a3[$v["lottery_fc3d_id"]] = $a3[$v["lottery_fc3d_id"]].'#'.$v["cal"];
		   }else{
			   $a3[$v["lottery_fc3d_id"]] = $v["cal"];
		   }
	   	}
	   	$a4 = array();
	   	foreach($lottery_kl8_detail as $v){
		   //内容
		   if(isset($a4[$v["lottery_kl8_id"]])){
			   $a4[$v["lottery_kl8_id"]] = $a4[$v["lottery_kl8_id"]].'#'.$v["cal"];
		   }else{
			   $a4[$v["lottery_kl8_id"]] = $v["cal"];
		   }
	   	}
	   	$a5 = array();
	   	foreach($lottery_pk10_detail as $v){
		   //内容
		   if(isset($a5[$v["lottery_pk10_id"]])){
			   $a5[$v["lottery_pk10_id"]] = $a5[$v["lottery_pk10_id"]].'#'.$v["cal"];
		   }else{
			   $a5[$v["lottery_pk10_id"]] = $v["cal"];
		   }
	   	}
		$a6 = array();
	   	foreach($lottery_ssc_detail as $v){
		   //内容
		   if(isset($a6[$v["lottery_ssc_id"]])){
			$a6[$v["lottery_ssc_id"]] = $a6[$v["lottery_ssc_id"]].'#'.$v["cal"];
		   }else{
			$a6[$v["lottery_ssc_id"]] = $v["cal"];
		   }
	  	}

		foreach($data as $k=>$v){
			foreach($class as $kk=>$vv){
				if($v["lotteryclass"] == $vv["id"]){
					$data[$k]["classname"] = $vv["name"];
				}
			}
		}

		$this->assign("caizhong",$caizhong);
		$this->assign("zhongjiang",$zhongjiang);
		$this->assign("yingli",$yingli);
		$this->assign("touzhu",$touzhu);
		$this->assign("count",$count);
		$this->assign("a1",$a1);
	    $this->assign("a2",$a2);
	    $this->assign("a3",$a3);
	    $this->assign("a4",$a4);
	    $this->assign("a5",$a5);
		$this->assign("a6",$a6);
		$this->assign("arr",$data);
		$this->assign("p",$p);
		$this->assign("totalpage",$totalpage);
		$this->assign("page",$page->show('Admin'));
		$this->assign("classs",$classs);
		$this->assign("accounts",$accounts);
		$this->assign("id",$id);
		$this->display();
	}
	//投注明细
	public function betDetail(){
		$id = I("id");
		$table = I("table");
		$lotteryclass = I("lotteryclass");

		$date1 =  M("lottery_add")->where("id  = {$id} and lotteryclass = {$lotteryclass}")->select();
		$date2 =  M("lottery_11x5")->where("id  = {$id} and lotteryclass = {$lotteryclass}")->select();
		$date3 =  M("lottery_fc3d")->where("id  = {$id} and lotteryclass = {$lotteryclass}")->select();
		$date4 =  M("lottery_kl8")->where("id  = {$id} and lotteryclass = {$lotteryclass}")->select();
		$date5 =  M("lottery_pk10")->where("id  = {$id} and lotteryclass = {$lotteryclass}")->select();
		$date6 =  M("lottery_ssc")->where("id  = {$id} and lotteryclass = {$lotteryclass}")->select();

		$date = array_merge($date1,$date2,$date3,$date4,$date5,$date6);

		foreach($date as $v){
			$classid[] = $v["lotteryclass"];
		}

		//期号
		foreach($date as $v){
		$qihao[] = $v["issue"];
	     }

       $qihaos = implode(",",$qihao);

		$arr = implode(",",$classid);
	  	if($arr){
		  	$dat =  M("lottery_class")->where("id in($arr)")->select();
	  	}
		foreach($dat as $v){
			$name[] = $v["name"];
		}

		//账户名
		$da = M('Accountsinfo')->where(" userid = '{$uid}' ")->find();
		$zhanghu = $da["accounts"];
        //彩种名称
        $names = implode(",",$name);
		//列表
		if($table=='1'){
		   $data = M("lottery_add_detail a")->where("lottery_add_id  = {$id}")->field("a.*,a.con_money zo_money")->select();
		}elseif($table=='2'){
			$data = M("lottery_11x5_detail")->where("lottery_11x5_id  = {$id}")->select();
		}elseif($table=='3'){
			$data = M("lottery_fc3d_detail")->where("lottery_fc3d_id  = {$id}")->select();
		}elseif($table=='4'){
			$data = M("lottery_kl8_detail")->where("lottery_kl8_id  = {$id}")->select();
		}elseif($table=='5'){
			$data = M("lottery_pk10_detail")->where("lottery_pk10_id  = {$id}")->select();
		}elseif($table=='6'){
			$data = M("lottery_ssc_detail")->where("lottery_ssc_id  = {$id}")->select();
		}
		$count = count($data);
		$this->assign("qihaos",$qihaos);
		$this->assign("zhanghu",$zhanghu);
		$this->assign("count",$count);
		$this->assign("names",$names);
       	$this->assign("arr",$date);
		$this->assign("arr1",$data);
		$this->assign("table",$table);
		$this->display();
	}
	//撤单
	public function che_order(){
		$uid = $_SESSION['user_id'];
		$table = I("table");
		$id = I("id");
		$p = I("p");
		if($table=='1'){
			$r = M("lottery_add")->where("id = $id")->field("money")->find();
			M("lottery_add")->where("id = $id")->save(array("status"=>1));
		}if($table=='2'){
			$r = M("lottery_11x5")->where("id = $id")->field("money")->find();
			M("lottery_11x5")->where("id = $id")->save(array("status"=>1));
		}if($table=='3'){
			$r = M("lottery_fc3d")->where("id = $id")->field("money")->find();
			M("lottery_fc3d")->where("id = $id")->save(array("status"=>1));
		}if($table=='4'){
			$r = M("lottery_kl8")->where("id = $id")->field("money")->find();
			M("lottery_kl8")->where("id = $id")->save(array("status"=>1));
		}if($table=='5'){
			$r = M("lottery_pk10")->where("id = $id")->field("money")->find();
			M("lottery_pk10")->where("id = $id")->save(array("status"=>1));
		}if($table=='6'){
			$r = M("lottery_ssc")->where("id = $id")->field("money")->find();
			M("lottery_ssc")->where("id = $id")->save(array("status"=>1));
		}
		M("Accountsinfo")->where("userid = $uid")->setInc("money",$r['money']);
		$info = M("Accountsinfo")->where("userid = $uid")->field("money")->find();
		$history_date = array(
			'number' => date("Ymd").rand(100000,999999),
			'type' => '12',
			'shou_money' => $r['money'],
			'ky_money' => $info['money'],
			'ctime' => time(),
			'uid' => $uid,
			'remark' => '投注撤单'
		);
		M("jiaoyi")->add($history_date);
		$this->success("撤单成功",U("Betting/index",array("p"=>$p)));
	}
}
?>